import json
from bazy.models import User
from bazy.models import Event
from bazy.models import OffenseType
from bazy.models import Hooligan
from bazy.login import Login
from django.http import HttpResponse
from bazy.forms import *
from bazy.forms import CreateOffenseTypeForm
from bazy.models import OffenseType

def getAll(request):
	log = Login(request)
	if log.isLogged() is not True:
		return HttpResponse(json.dumps([{'Error':-1}]))
	eventsArray = Event.objects.all()
	eventsList = list()
	for event in eventsArray:
		eventsList.append({'id':event.id,'offenseType':event.offenseType.name,'username':event.user.name,'coordinatesWidth':event.coordinatesWidth,'coordinatesHeight':event.coordinatesHeight,'date':event.date.strftime('%Y-%m-%d %H:%M:%S'),})
	return HttpResponse(json.dumps(eventsList))


def create(request):
	log = Login(request)
	if log.isLogged() is not True:
		return HttpResponse(json.dumps([{'Error':-1}]))
	form = CreateEventForm(request.POST)
	if form.is_valid() is False:
		return HttpResponse(json.dumps([{'Error':-2}]))
	OffenseType.objects.get(pk=request.POST.get('offenseType',0))
	event = Event(offenseType=OffenseType.objects.get(pk=request.POST.get('offenseType',0)),user=User.objects.get(pk=request.POST.get('user',0)),coordinatesWidth=request.POST.get('coordinatesWidth',0),coordinatesHeight=request.POST.get('coordinatesHeight',0),date=request.POST.get('date'))
	hooligans=request.POST.getlist('hooligans') 
	event.save()
	for hooligan in hooligans:
		hool = Hooligan.objects.get(pk=hooligan)
		event.hooligans.add(hool)
	event.save()
	return HttpResponse(json.dumps([{'Error':0}]))

def edit(request):
	log = Login(request)
	if log.isLogged() is not True:
		return HttpResponse(json.dumps([{'Error':-1}]))
	form = CreateEventForm(request.POST)
	if form.is_valid() is False:
		return HttpResponse(json.dumps([{'Error':-2}]))
	OffenseType.objects.get(pk=request.POST.get('offenseType',0))
	event= Event.objects.get(pk=request.POST.get('event_id',0))
	event.offenseType=OffenseType.objects.get(pk=request.POST.get('offenseType',0))
	event.user=User.objects.get(pk=request.POST.get('user',0))
	event.coordinatesWidth=request.POST.get('coordinatesWidth',0)
	event.coordinatesHeight=request.POST.get('coordinatesHeight',0)
	event.date=request.POST.get('date')
	hooligans=request.POST.getlist('hooligans') 
	event.save()
	for hooligan in hooligans:
		hool = Hooligan.objects.get(pk=hooligan)
		event.hooligans.add(hool)
	event.save()
	return HttpResponse(json.dumps([{'Error':0}]))

def delete(request):
	log = Login(request)
	if log.isLogged() is not True:
		return HttpResponse(json.dumps([{'Error':-1}]))
	if request.method != 'POST':
		return HttpResponse(json.dumps([{'Error':-2}]))
	event= Event.objects.get(pk=request.POST.get('event_id',0))
	event.delete()
	return HttpResponse(json.dumps([{'Error':0}]))

def getById(request):
	log = Login(request)
	if log.isLogged() is not True:
		return HttpResponse(json.dumps([{'Error':-1}]))
	if request.method != 'POST':
		return HttpResponse(json.dumps([{'Error':-2}]))
	event = Event.objects.get(pk=request.POST.get('id',0))
	return HttpResponse(json.dumps([{'id':event.id,
								  'offenseType':event.offenseType.name,
								  'username':event.user.name,
								  'user':event.user.id,
								  'coordinatesWidth':event.coordinatesWidth,
								  'coordinatesHeight':event.coordinatesHeight,
								  'date':event.date.strftime('%Y-%m-%d %H:%M:%S'),}]))

def getByHooliganId(request):

	log = Login(request)
	if log.isLogged() is not True:
		return HttpResponse(json.dumps([{'Error':-1}]))
	if request.method != 'POST':
		return HttpResponse(json.dumps([{'Error':-2}]))
	events = Event.objects.filter(hooligans__pk=request.POST.get('id',0))
	eventsList=list()
	for event in events:
		eventsList.append({'id':event.id,
								  'offenseType':event.offenseType.name,
								  'username':event.user.name,
								  'coordinatesWidth':event.coordinatesWidth,
								  'coordinatesHeight':event.coordinatesHeight,
								  'date':event.date.strftime('%Y-%m-%d %H:%M:%S')})
	return HttpResponse(json.dumps(eventsList))

def addHooliganToEvent(request):
	log = Login(request)
	if log.isLogged() is not True:
		return HttpResponse(json.dumps([{'Error':-1}]))
	if request.method != 'POST':
		return HttpResponse(json.dumps([{'Error':-2}]))
	form = AddHooliganToEvent(request.POST)
	if form.is_valid() is not True:
		return HttpResponse(json.dumps([{'Error':-2}]))
	event= Event.objects.get(pk=request.POST.get('event_id',1))
	hooligans=request.POST.getlist('hooligans')
	for hooligan in hooligans:
		hool = Hooligan.objects.get(pk=hooligan)
		event.hooligans.add(hool)
	event.save()
	return HttpResponse(json.dumps([{'Error':-0}]))
#class Event (models.Model):
#	offenseType =  models.ForeignKey(OffenseType)
#	user = models.ForeignKey('User')
#	hooligans = models.ManyToManyField('Hooligan')
#	coordinatesWidth= models.FloatField()
#	coordinatesHeight= models.FloatField()
#	date= models.DateTimeField()